﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
using libConn;

namespace ApartmentSale
{
	public partial class AffirmanceRuleInput : Window
	{
		public AffirmanceRuleInput()
		{
			this.InitializeComponent();

            opCommand = OpCommand.Create;
            gData = new AffirmanceRuleView();
		}

        public AffirmanceRuleInput(AffirmanceRuleView data)
            :this()
        {
            opCommand = OpCommand.Update;
            tbName.IsEnabled = false;
            gData = data;
            getData(data);
        }

        private enum OpCommand { Create, Update }
        private OpCommand opCommand;
        private AffirmanceRuleView gData;

        private void getData(AffirmanceRuleView data)
        {
            tbName.Text = data.AffirmanceRuleType;
            tbAmount.Text = data.AffirmanceRuleAmount.ToString();
            tbRule.Text = data.AffirmanceRuleRule.ToString();
        }

        private bool checkSameName(AffirmanceRuleView data)
        {
            string errMessage = null;
            string str = "SELECT * FROM affirmance_rules WHERE affirmance_rule_type = '"
                + data.AffirmanceRuleType.Replace("'", "''") + "'";

            DataTable oleDT;
            oleDT = libConn.AccdbConn.query(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "Check the rules of the same name affirmance fail",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            if (oleDT.Rows.Count > 0)
            {
                System.Windows.MessageBox.Show(
                    "Rules of the same name exists affirmance",
                    "Save faild",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Stop);
                return false;
            }

            return true;
        }

        private bool insertData(AffirmanceRuleView data)
        {
            if (!checkSameName(data))
                return false;

            string errMessage = null;

            string str = "INSERT INTO affirmance_rules (affirmance_rule_type, affirmance_rule_amount, affirmance_rule_rule) VALUES ('";
            str += data.AffirmanceRuleType.Replace("'", "''") + "', ";
            str += data.AffirmanceRuleAmount + ",";
            str += data.AffirmanceRuleRule + ")";

            libConn.AccdbConn.exec(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "Save faild",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            MessageBox.Show("Add a new rule success.");
            return true;
        }

        private bool updateData(AffirmanceRuleView data)
        {
            string errMessage = null;

            string str = "UPDATE affirmance_rules SET affirmance_rule_type = '" + data.AffirmanceRuleType.Replace("'", "''") + "', ";
            str += "affirmance_rule_amount = " + data.AffirmanceRuleAmount + ", ";
            str += "affirmance_rule_rule = " + data.AffirmanceRuleRule + " ";
            str += "WHERE affirmance_rule_id = " + data.ID;

            libConn.AccdbConn.exec(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "Modify faild",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            return true;
        }

        private void btOk_Click(object sender, RoutedEventArgs e)
        {
            if (tbName.Text.Length == 0)
            {
                MessageBox.Show("Please enter a name for the rule.");
                tbName.Focus();
                return;
            }
            
            gData.AffirmanceRuleType = tbName.Text;

            double amount;
            if (tbAmount.Text.Length == 0 || !double.TryParse(tbAmount.Text, out amount) || amount <= 1)
            {
                MessageBox.Show("Please enter a valid number, the deductible amount must be greater than 1 times.");
                tbAmount.Focus();
                return;
            }

            gData.AffirmanceRuleAmount = amount;

            double rule;
            if (tbRule.Text.Length == 0 || !double.TryParse(tbRule.Text, out rule) || rule <= 0)
            {
                MessageBox.Show("Please enter a valid number.");
                tbRule.Focus();
                return;
            }

            gData.AffirmanceRuleRule = rule;

            if (opCommand == OpCommand.Create)
            {
                if (insertData(gData))
                {
                    this.Close();
                }
            }
        }

        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
	}
}